﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ChangeHope.Common;

namespace YXShop.Web.filehandle
{
    public partial class updatepacket : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                //程序更新
                string url = System.Configuration.ConfigurationManager.AppSettings["UpdateAdress"].ToString() + "vesion.html";
                string lastestVesion = ChangeHope.Common.NetWorkHelper.ReadPageContentByUrl(url, "GBK").Trim();
                FileHelper hf = new FileHelper();
                string vesion = hf.ReadFileContent(HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"] + "admin/vesion.ini").Trim();
                string isUpdateTemlete = this.Request.QueryString["UpdateTemlete"] == null ? "0" : this.Request.QueryString["UpdateTemlete"].ToString();
                if (vesion.Equals(lastestVesion))
                {
                    Response.Write("已经是最新版本，无需更新。");
                }
                else
                {

                    string Packet=UpdatePacket(isUpdateTemlete);
                    string DataBase=UpdateDataBase();
                    Response.Write(Packet + DataBase);
                }
            }
        }

        private string UpdatePacket(string isUpdateTemlete)
        {
            string reStr = string.Empty;
            //获取相关地址
            string updateAdress = System.Configuration.ConfigurationManager.AppSettings["UpdateAdress"].ToString();
            string packetUrl = updateAdress + "updatepacket.zip";
            //下载相关文件到对应地址
            System.Net.WebClient webClient = new System.Net.WebClient();
            string zipFile = Request.ServerVariables["APPL_PHYSICAL_PATH"] + "UpdatePacket.zip";
            webClient.DownloadFile(packetUrl, zipFile);
            ZipHelper zh = new ZipHelper();
            if (zh.UnZip(zipFile, Request.ServerVariables["APPL_PHYSICAL_PATH"]))
            {
               reStr="程序已更新完毕!";
            }
            
            //以下是更新模版
            if (isUpdateTemlete.Equals("1"))
            {
                string templetsUrl = updateAdress + "templates.zip";
                string templetsFile = Request.ServerVariables["APPL_PHYSICAL_PATH"] + "templates.zip";
                webClient.DownloadFile(templetsUrl, templetsFile);
                if (zh.UnZip(templetsFile, Request.ServerVariables["APPL_PHYSICAL_PATH"]))
                {
                    reStr+="模板已更新完毕!";
                }
            }
            webClient.Dispose();
            webClient = null;
            return reStr;
        }

        private string UpdateDataBase()
        {
            string reStr = string.Empty;
            string sqlUrl = System.Configuration.ConfigurationManager.AppSettings["UpdateAdress"].ToString() + "sql.txt";
            string sqls = ChangeHope.Common.NetWorkHelper.ReadPageContentByUrl(sqlUrl, "GBK");

            YXShop.BLL.SystemInfo.UpgradeDataBase updateDataBase = new YXShop.BLL.SystemInfo.UpgradeDataBase();
            if (updateDataBase.UpdateDataBaseByVesion(sqls))
            {
                reStr = "数据库更新完成！";
            }
            else
            {
                reStr = "数据库更新出现错误!";
            }
            updateDataBase.Dispose();
            updateDataBase = null;
            return reStr;
        }
    }
}
